<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Modal\memberModal;
use App\Admin\Repositories\SegmentSentenceRecord;
use App\Admin\Repositories\TranslateLanguage;
use App\Admin\Repositories\TranslateType;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class SegmentSentenceRecordController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SegmentSentenceRecord(), function (Grid $grid) {
            $grid->export();
            $grid->tools(function  (Grid\Tools  $tools)  {
                //Excel导入
                $tools->append(new  memberModal());
            });
            $grid->column('id')->sortable();
//            $grid->column('types.name','类型');
//            $grid->column('source_language_code')->display(function (){
//                return TranslateLanguage::make()->getNameByCode($this->source_language_code);
//            });
            $grid->column('source_name');
//            $grid->column('source_file');
//            $grid->column('target_language_code')->display(function (){
//                return TranslateLanguage::make()->getNameByCode($this->target_language_code);
//            });
            $grid->column('target_name');
//            $grid->column('target_file');
//            $grid->column('user_id');


//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel()->expand();
//                $filter->equal('type','类型')->select(TranslateType::make()->getAllMap())->width(3);
//                $filter->equal('source_language_code','原文语言')->select(TranslateLanguage::make()->getAllMap())->width(3);
//                $filter->equal('target_language_code','译文语言')->select(TranslateLanguage::make()->getAllMap())->width(3);
                $filter->where('search1', function ($query) {

                    $query->where('source_name', 'like', "%{$this->input}%");

                },'原文文本')->placeholder('')->width(3);
                $filter->where('search2', function ($query) {

                    $query->where('target_name', 'like', "%{$this->input}%");

                },'译文文本')->placeholder('')->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SegmentSentenceRecord(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('source_name');
            $show->field('source_file');
            $show->field('target_name');
            $show->field('target_file');
            $show->field('user_id');
            $show->field('source_language_code');
            $show->field('target_language_code');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SegmentSentenceRecord(), function (Form $form) {
            $form->display('id');
            $form->select('type')->options(TranslateType::make()->getAllMap());
            $form->select('source_language_code')->options(TranslateLanguage::make()->getAllMap());
            $form->textarea('source_name');
//            $form->text('source_file');
            $form->select('target_language_code')->options(TranslateLanguage::make()->getAllMap());
            $form->textarea('target_name');
//            $form->text('target_file');
//            $form->text('user_id');



            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
